#include "mainwindow.h"
#include "ui_mainwindow.h"


#define POINT_  120

int offset_x=250;
int offset_y=250;

#if 1
//定义一个3D的图形

float cube[8][3]=   {{-POINT_,-POINT_,-POINT_},\
                    {-POINT_,POINT_,-POINT_},\
                    {POINT_,POINT_,-POINT_},\
                    {POINT_,-POINT_,-POINT_},\
                    {-POINT_,-POINT_,POINT_},\
                    {-POINT_,POINT_,POINT_},\
                    {POINT_,POINT_,POINT_},\
                    {POINT_,-POINT_,POINT_}};//立方体坐标


//连接顺序
char    line_cude[12][2]=   {{0, 1},\
                            {1, 2},\
                            {2, 3},\
                            {3, 0},\
                            {4, 5},\
                            {5, 6},\
                            {6, 7},\
                            {7, 4},\
                            {0, 4},\
                            {1, 5},\
                            {2, 6},\
                            {3, 7}};

float dr_buf[24][4]={0};

#endif



#if 0
//新增纸飞机代码如下
static float cube[5][3] ={{-POINT_,0,-POINT_}, \
                      {0,0,POINT_}, \
                      {POINT_,0,-POINT_}, \
                      {0,0,-POINT_}, \
                      {0,POINT_,-POINT_}
};
float cube_temp[5][3]={0};
static char line_cude[7][2] =   {{0, 1}, \
                                {1, 2}, \
                                {2, 3}, \
                                {3, 4}, \
                                {4, 1}, \
                                {1, 3}, \
                                {3, 0}
};
float dr_buf[7][4]={0};
#endif


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    x0=0;
    y0=0;
    x1=0;
    y1=0;

    QTimer * timer = new QTimer(this);

    QObject::connect(timer,SIGNAL(timeout()),this,SLOT(timerSlot()));

    timer->start(100);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{

    painter.begin(this);
    painter.setRenderHint(QPainter::Antialiasing,true);//设置反锯齿
    pen.setWidth(5);
    pen.setColor(QColor(255,0,0));
    painter.setPen(pen);//设置笔的大小
    //painter.drawLine(offset_x+x0,offset_y+y0,offset_x+x1,offset_y+y1);//起点和终点
    for(int i=0;i<12;i++)
    {
        painter.drawLine(offset_x+dr_buf[i][0],offset_y+dr_buf[i][1],offset_x+dr_buf[i][2],offset_y+dr_buf[i][3]);
    }
    painter.end();

}

//计算矩阵算法
float *MainWindow::matconv(float *a,float b[3][3])
{
        float res[3];
        int i;
        for(i=0;i<3;i++){
         res[i]=b[i][0]*a[0]+b[i][1]*a[1]+b[i][2]*a[2];
        }
        for(i=0;i<3;i++){
         a[i]=res[i];
        }
        return a;
}


//旋转这个向量
void MainWindow::rotate(float *obj,float x,float y,float z)
{


//    float rz[3][3];
//    float ry[3][3];
//    float rx[3][3];
//    rz[0][0]=cos(z),rz[0][1]=-sin(z),   rz[0][2]=0;
//    rz[1][0]=sin(z),rz[1][1]= cos(z),   rz[1][2]=0;
//    rz[2][0]=0,		rz[2][1]=0,         rz[2][2]=1;

//    ry[0][0]=1,     ry[0][1]=0,         ry[0][2]=0;
//    ry[1][0]=0,     ry[1][1]=cos(y),    ry[1][2]=-sin(y);
//    ry[2][0]=0,	    ry[2][1]=sin(y),    ry[2][2]=cos(y);

//    rx[0][0]=cos(x), rx[0][1]=0,			rx[0][2]=sin(x);
//    rx[1][0]=0,		 rx[1][1]=1,			rx[1][2]=0;
//    rx[2][0]=-sin(x),rx[2][1]=0,		rx[2][2]=cos(x);

    float rz[3][3]={{cos(z),-sin(z),0},{sin(z),cos(z),0},{0,0,1}};
    float ry[3][3]={{1,0,0},{0,cos(y),-sin(y)},{0,sin(y),cos(y)}};
    float rx[3][3]={{cos(x),0,sin(x)},{0,1,0},{-sin(x),0,cos(x)}};
    matconv(matconv(matconv(obj,rz),ry),rx);
}

void MainWindow::show_3d__quadrangle(float x,float y,float z)
{

    char i=0;
    char j=0;

    float pi =3.1415;

    short x0,y0,x1,y1;

    for(i=0;i<8;i++)
    {
        rotate(cube[i],pi/100,pi/100,pi/100);

    }

    //取坐标点，一共12条线
    //坐标是3维对2维的投影，只取想x，y轴坐标，z轴丢弃
    for(i=0;i<12;i++)
    {
        x0=cube[line_cude[i][0]][0];
        y0=cube[line_cude[i][0]][1];

        x1=cube[line_cude[i][1]][0];
        y1=cube[line_cude[i][1]][1];




        dr_buf[i][0]=x0;
        dr_buf[i][1]=y0;
        dr_buf[i][2]=x1;
        dr_buf[i][3]=y1;


        update();

    }


}

void MainWindow::show_3d_triangle(float x,float y,float z)
{

    char i=0;
    char j=0;

    float pi =3.1415;

    short x0,y0,x1,y1;

    for(i=0;i<5;i++)
    {
        rotate(cube[i],pi/100,pi/100,pi/100);

    }


    for(i=0;i<7;i++)
    {
        x0=cube[line_cude[i][0]][0];
        y0=cube[line_cude[i][0]][1];

        x1=cube[line_cude[i][1]][0];
        y1=cube[line_cude[i][1]][1];


        dr_buf[i][0]=x0;
        dr_buf[i][1]=y0;
        dr_buf[i][2]=x1;
        dr_buf[i][3]=y1;

        update();

    }

}

void MainWindow::timerSlot()
{
    //show_3d_triangle(0,0,0);
    show_3d__quadrangle(0,0,0);
}
